<?php

namespace app\admin\model;

use think\Model;

use think\Db;
use think\Request;

class Fin_account extends Model
{
    public function getfinaccount($res)
    {
        $lang = $res['lang'];
        $map = array();
        if (isset($res['country_id']) && $res['country_id']) {
            $map['country_id'] = $res['country_id'];
        }
        $datares = $this->where($map)->select();
        //数据结构
        $data = getTree($datares);
        foreach ($data as $datakey => $dataval) {
            if ($dataval['adjust'] == '0') {
                //不调整
                $data[$datakey]['adjust'] = '=';
            } elseif ($dataval['adjust'] == '1') {
                //收入
                $data[$datakey]['adjust'] = '+';
            } elseif ($dataval['adjust'] == '2') {
                //支出
                $data[$datakey]['adjust'] = '-';
            }
            //指定职务
            $data[$datakey]['positioname'] = $this->getposition($dataval['position_ids'], $lang);
            $data[$datakey]['update_date'] = getTimeDate($dataval['update_at'],12);//更新时间
            //编辑者
            $data[$datakey]['updatename'] = getinfo($dataval['update_id'], 'admin')['nickname'];
            $infoaccoutin = $this->where('pid', $dataval['id'])->find();
            if ($infoaccoutin) {
                $data[$datakey]['isexist'] = 1;
            } else {
                $data[$datakey]['isexist'] = 2;
            }
            $data[$datakey]['level'] = str_repeat('&emsp;', $dataval['level'] * 1);
        }
        $count = $this->where($map)->count();
        return array('count' => $count, 'data' => $data);
    }

    //获取对应语言下的职位名称
    protected function getposition($position_ids, $lang)
    {
        $positionarr = explode(',', $position_ids);
        foreach ($positionarr as $position_idskey => $position_idsval) {
            $where['mkey'] = 'position_' . $position_idsval;
            //对应下的语言名称
            $positionname[] = getlangname('multi_lang', $where, $lang);
        }
        return implode('/', $positionname);
    }

    //获取对应国家的上级目录
    public function finaccounts($res)
    {
        return getTree($this->where('country_id', $res['country_id'])->select());
    }

    //设置权限的写入
    public function setaccroot($data)
    {
        $name = $this->where('id', $data['id'])->value('name');
        $position_ids = array_unique(array_filter($data['position_ids']));
        if (empty($position_ids)) {
            return array('msg' => lang("job_authorized_can't_be_empty"), 'status' => 2);
        }
        $admin = session('admin');
        if ($admin['position_id'] != 1) {
            $country_id = getinfo($admin['position_id'], 'position')['country_id'];
            $where['country_id'] = $country_id;
            $positionarr = array_column(getables('position', $where), 'id');
            //不是全球管理员
            $parr = explode(',', $this->where('id', $data['id'])->value('position_ids'));//之前的权限
            foreach ($parr as $parrkey => $parrval) {
                if (in_array($parrval, $positionarr)) {
                    unset($parr[$parrkey]);
                }
            }
            //去重 去空 合并
            $posids = array_unique(array_filter(array_merge($parr, $position_ids)));
            $updata['position_ids'] = implode(",", $posids);
        } else {
            //是全球管理员
            $updata['position_ids'] = implode(',', $position_ids);//去重
        }
        $accountid = $this->where('id', $data['id'])->update($updata);
        if ($accountid !== false) {
            Db::commit();
            return array('msg' => $name . lang("authorization_succeeded"), 'status' => 1);
        } else {
            return array('msg' => $name . lang("authorization_failed"), 'status' => 0);
        }
    }

    //会计科目添加
    public function finaccountaddedit($data)
    {
        $ckdata = notEmpty(['name' => lang("subject_name")]);
        if ($ckdata['status']==2) {
            return $ckdata;
        }
        $name = $data['name'] = trim($data['name']);
        $act = $data['act'];
        unset($data['act']);
        if ($act == 'finaccountadd') {
            $data['create_at'] = $data['update_at'] = time();
            $data['create_id'] = $data['update_id'] = session('userid');
            $acretid = $this->insertGetId($data);
            if ($acretid) {
                return array('msg' => $name . lang("added_successfully"), 'status' => 1);
            } else {
                return array('msg' => $name . lang("add_failed"), 'status' => 0);
            }
        } elseif ($act == 'finaccountedit') {
            $data['update_at'] = time();
            $data['update_id'] = session('userid');
            $accid = $this->where('id', $data['id'])->update($data);
            if ($accid !== false) {
                return array('msg' => $name . lang("edited_successfully"), 'status' => 1);
            } else {
                return array('msg' => $name . lang("edit_failed"), 'status' => 0);
            }
        }
    }

    //会计科目的删除
    public function finaccountdel($data)
    {
        $name = $data['name'];
        $dpres = $this->where('pid', $data['id'])->find();
        if ($dpres) {
            return array('msg' => $name . lang("contains_sublevel_doesn't_allow_deletion"), 'status' => 0);
        }
        //含有记账表的数据无法删除
        $jounid = Db::name('journal')->where('account_id', $data['id'])->select();
        if ($jounid){
            return array('msg' => $name . lang("contains_data_and_can't_be_deleted"), 'status' => 0);
        }
            $retdelid = $this->where('id', $data['id'])->delete();//删除会计表
        if ($retdelid) {
            return array('msg' => $name . lang("deleted_successfully"), 'status' => 1);
        } else {
            return array('msg' => $name . lang("failed_to_delete"), 'status' => 0);
        }
    }
}